﻿using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;

namespace Songhay.OpenXml.Transformations
{
    using Songhay.OpenXml.Extensions;
    using Songhay.OpenXml.Models;

    /// <summary>
    /// Transforms the Open XML WordML <c>hyperlink</c> element.
    /// </summary>
    [Export(typeof(IWordProcessingDocumentTransformation))]
    public class HyperlinkTransformation : IWordProcessingDocumentTransformation
    {
        #region IWordProcessingDocumentTransformation Members

        /// <summary>
        /// Gets or sets the execution ordinal.
        /// </summary>
        /// <value>The execution ordinal.</value>
        public byte ExecutionOrdinal { get; set; }

        /// <summary>
        /// Determines whether this instance can transform the specified word doc.
        /// </summary>
        /// <param name="wordDoc">The Word document.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="node">The node.</param>
        /// <returns>
        /// 	<c>true</c> if this instance can transform the specified word doc; otherwise, <c>false</c>.
        /// </returns>
        public bool CanTransform(WordprocessingDocument wordDoc,
            SonghayHtmlConverterSettings settings, XNode node)
        {
            return this.HasElementName(node, SonghayW.hyperlink);
        }

        /// <summary>
        /// Transforms the specified Word document.
        /// </summary>
        /// <param name="wordDoc">The Word document.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="node">The node.</param>
        /// <param name="imageHandler">The image handler.</param>
        public object Transform(WordprocessingDocument wordDoc,
            SonghayHtmlConverterSettings settings, XNode node,
            Func<ImageInfo, XElement> imageHandler)
        {
            XElement element = node as XElement;
            if(element.Attribute(SonghayR.id) == null) return null;
            try
            {
                var uri = wordDoc.MainDocumentPart
                    .HyperlinkRelationships
                    .Where(x => x.Id == (string)element.Attribute(SonghayR.id))
                    .First().Uri;
                var attrUri = new XAttribute(SonghayXhtml.href, uri);

                var title = element.Attribute(SonghayW.tooltip);
                var attrTitle = ((title == null) || string.IsNullOrEmpty(title.Value)) ?
                    null : new XAttribute(SonghayXhtml.title_attr, title.Value);

                return new XElement(SonghayXhtml.a, attrUri, attrTitle,
                    OpenXmlUtility.TransformPackageNode(wordDoc, settings,
                        element.Element(SonghayW.r), imageHandler));
            }
            catch(UriFormatException)
            {
                return element.Elements()
                    .Select(e => OpenXmlUtility.TransformPackageNode(wordDoc, settings, e, imageHandler));
            }
        }

        #endregion
    }
}
